<!DOCTYPE html>
<html>
	<head>
		<title>Revel Test Runner</title>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
		<link href="/@tests/public/css/bootstrap.css" type="text/css" rel="stylesheet"></link>
		<script src="/@tests/public/js/jquery-1.9.1.min.js" type="text/javascript"></script>
		<style>
		header { padding:20px 0; background-color:#ADD8E6 }
		.passed td { background-color: #90EE90 !important; }
		.failed td { background-color: #FFB6C1 !important; }
		.tests td.name, .tests td.result { padding-top: 13px; }
		pre { font-size:10px; white-space: pre; }
		</style>
	</head>
	<body>
		<header>
			<div class="container">
				<table><tr><td>
					<h1>Test Runner</h1>
					<p class="lead">Run all of your application's tests from here.</p>
				</td><td style="padding-left:150px;">
					<button class="btn btn-large" all-tests="">Run All Tests</button>
				</td></tr></table>
			</div>
		</header>

		<div class="container">
			{{range .testSuites}}
				<p class="lead" style="margin-top:20px;">{{.Name}}</p>
				<table class="table table-striped tests" suite="{{.Name}}">
					{{range .Tests}}
						<tr>
							<td class="name">{{.Name}}</td>
							<td class="result">
							</td>
							<td><button test="{{.Name}}" class="btn">Run</button></td>
						</tr>
					{{end}}
				</table>
			{{end}}
		</div>

<script>
var running = 0;

$("button[test]").click(function() {
	var button = $(this).addClass("disabled").text("Running");
	running += 1;
	runTest(button);
});

$("button[all-tests]").click(function() {
	var button = $(this).addClass("disabled").text("Running");
	$("button[test]").click();
});

function runTest(button) {
	var suite = button.parents("table").attr("suite");
	var test = button.attr("test");
	var row = button.parents("tr");
	var resultCell = row.children(".result");
	$.ajax({
		dataType: "json",
		url: "/@tests/"+suite+"/"+test,
		async: false,
		success: function(result) {
			row.attr("class", result.Passed ? "passed" : "failed");
			if (result.Passed) {
				resultCell.html("");
			} else {
				resultCell.html(result.ErrorHtml);
			}
			button.removeClass("disabled").text("Run");
			running -= 1;
			if (running == 0) {
				$("button[all-tests]").removeClass("disabled").text("Run All Tests");
			}
		}});
}
</script>

	</body>
</html>
